﻿using API;
using API.Product;
using Common;
using DBUtility;
using DevExpress.Xpo;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
    public class BrandBLL : BaseBLL
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="lastUpdateDate"></param>
        public void BatchInsert(DateTime? lastUpdateDate = null)
        {
            try
            {
                this.Request.LastUpdateDate = lastUpdateDate;
                ApiProduct dataProxy = ServiceProxy.ApiProductProxy();
                MP_Brand[] brands = dataProxy.GetBrands(this.Request);
                if (brands == null || brands.Length <= 0) return;
                using (UnitOfWork uow = DatabaseSchema.UnitOfWork)
                {
                    foreach (MP_Brand b in brands)
                    {
                        P_Brand brand = new P_Brand(uow);
                        brand.BrandID = b.BrandID;
                        brand.BrandName = b.BrandName;
                        brand.StatusID = b.StatusID;
                        brand.Sort = b.Sort;
                        brand.Creator = b.Creator;
                        brand.CreateDT = b.CreateDT;
                        brand.Operator = b.Operator;
                        brand.UpdateDT = b.UpdateDT.Value;
                        brand.Remark = b.Remark;
                        brand.Save();
                    }
                    uow.CommitChanges();
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("BrandBLL.BatchInsert", ex);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public DateTime MaxUpdateDate() 
        {
            string sql = @"select Max(UpdateDT) from P_Brand";
            object updateDate=DatabaseSchema.Session.ExecuteScalar(sql);
            if(updateDate!=null)
                return Convert.ToDateTime(updateDate);
            return DateTime.Now.AddYears(-5);
        }
    }
}
